home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / plan / src / popup.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  267 lines

  1. /*
  2.  * Various popups, such as the About... dialog.
  3.  *
  4.  *    create_about_popup()
  5.  *                    Create About info popup
  6.  *    create_error_popup(widget, error, fmt, ...)
  7.  *                    Create error popup with Unix error
  8.  *    create_nodaemon_popup()
  9.  *                    Create no-daemon warning popup
  10.  *    create_multiple_popup()
  11.  *                    Create multiple-plan's warning popup
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <time.h>
  17. #include <string.h>
  18. #ifndef VARARGS
  19. #include <stdarg.h>
  20. #endif
  21. #include <sys/types.h>
  22. #include <Xm/Xm.h>
  23. #include <Xm/MessageB.h>
  24. #include <Xm/Protocols.h>
  25. #include "cal.h"
  26. #include "version.h"
  27.  
  28. #define NODAEMON_ONCE        /* if defined, the error popup that offers */
  29.                 /* to start pland comes up only once. */
  30. static Boolean suppress_daemon_warning = False;
  31.  
  32. #ifndef __DATE__
  33. #define __DATE__ ""
  34. #endif
  35.  
  36. extern BOOL        find_file();
  37. extern char        *progname;    /* argv[0] */
  38. extern Display        *display;    /* everybody uses the same server */
  39. extern Widget        mainwindow;    /* popup menus hang off main window */
  40. extern BOOL        interactive;    /* interactive or fast appt entry? */
  41.  
  42.  
  43. /*---------------------------------------------------------- about ----------*/
  44. static char about_message[] = "\
  45. \n\
  46. Day Planner version %s %s\n\
  47. Compiled %s\n\n\
  48. Author: Thomas Driemeyer <thomas@bitrot.in-berlin.de>\n\n\
  49. PostScript output by Karl Bunch <karl@ttank.ttank.com>\n\
  50. Japanese version by Ogura Yoshito <ogura@ndc.ngsk.sony.co.jp>\
  51. \n";
  52.  
  53.  
  54. create_about_popup()
  55. {
  56.     char            msg[512];
  57.     Widget            dialog;
  58.     XmString        s;
  59.     Arg            args[10];
  60.     int            n;
  61.  
  62.     sprintf(msg, about_message, VERSION, PATCHLEVEL, __DATE__);
  63.     s = XmStringCreateLtoR(msg, XmSTRING_DEFAULT_CHARSET);
  64.     n = 0;
  65.     XtSetArg(args[n], XmNmessageString,    s);            n++;
  66.     dialog = XmCreateInformationDialog(mainwindow, "About", args, n);
  67.     XmStringFree(s);
  68.     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
  69.     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
  70.     (void)XmInternAtom(display, "WM_DELETE_WINDOW", False);
  71.     XtManageChild(dialog);
  72. }
  73.  
  74.  
  75. /*---------------------------------------------------------- errors ---------*/
  76. /*VARARGS*/
  77. #ifndef VARARGS
  78. create_error_popup(Widget widget, int error, char *fmt, ...)
  79. {
  80.     va_list            parm;
  81. #else
  82. create_error_popup(widget, error, fmt, a, b, c, d)
  83.     Widget            widget;
  84.     int            error, a, b, c, d;
  85.     char            *fmt;
  86. {
  87. #endif
  88.     char            msg[1024];
  89.     Widget            dialog;
  90.     XmString        string;
  91.     Arg            args;
  92.  
  93.     strcpy(msg, "ERROR:\n\n");
  94.  
  95. #ifndef VARARGS
  96.     va_start(parm, fmt);
  97.     vsprintf(msg + strlen(msg), fmt, parm);
  98.     va_end(parm);
  99. #else
  100.     sprintf(msg + strlen(msg), fmt, a, b, c, d);
  101. #endif
  102. #    ifdef sgi
  103.     if (error) {
  104.         strcat(msg, "\n");
  105.         strcat(msg, sys_errlist[error]);
  106.     }
  107. #    endif
  108.     if (!interactive) {
  109.         fputs(msg, stderr);
  110.         return;
  111.     }
  112.     string = XmStringCreateLtoR(msg, XmSTRING_DEFAULT_CHARSET);
  113.     XtSetArg(args, XmNmessageString, string);
  114.     dialog = XmCreateWarningDialog(widget, "Error", &args, 1);
  115.     XmStringFree(string);
  116.     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
  117.     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
  118.     (void)XmInternAtom(display, "WM_DELETE_WINDOW", False);
  119.     XtManageChild(dialog);
  120. }
  121.  
  122.  
  123. /*---------------------------------------------------------- no daemon ------*/
  124. static char nodaemon_message[] = "\
  125. Warning:\n\
  126. There is no Scheduler daemon. Without a daemon,\n\
  127. no action will be taken when an alarm triggers.\n\
  128. Please start \"%s\" in your ~/.xsession or ~/.sgisession file.";
  129.  
  130. static void ok_callback(), cancel_callback();
  131.  
  132. create_nodaemon_popup()
  133. {
  134.     char            msg[512];
  135.     Widget            dialog;
  136.     XmString        s1, s2, s3;
  137.     Arg            args[10];
  138.     int            n;
  139.  
  140. #ifdef NODAEMON_ONCE
  141.     if (suppress_daemon_warning)
  142.         return;
  143. #endif
  144.     sprintf(msg, nodaemon_message, DAEMON_FN);
  145.     s1 = XmStringCreateLtoR(msg, XmSTRING_DEFAULT_CHARSET);
  146.     s2 = XmStringCreateSimple("Start daemon now");
  147.     s3 = XmStringCreateSimple("Continue without daemon");
  148.     n = 0;
  149.     XtSetArg(args[n], XmNmessageString,    s1);        n++;
  150.     XtSetArg(args[n], XmNokLabelString,    s2);        n++;
  151.     XtSetArg(args[n], XmNcancelLabelString,    s3);        n++;
  152.     dialog = XmCreateWarningDialog(mainwindow, "Warning", args, n);
  153.     XmStringFree(s1);
  154.     XmStringFree(s2);
  155.     XmStringFree(s3);
  156.     XtAddCallback(dialog, XmNokCallback, ok_callback, (XtPointer)NULL);
  157.     XtAddCallback(dialog, XmNcancelCallback, cancel_callback,
  158.                               (XtPointer)NULL);
  159.     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
  160.     (void)XmInternAtom(display, "WM_DELETE_WINDOW", False);
  161.     XtManageChild(dialog);
  162. }
  163.  
  164.  
  165. /*
  166.  * start the daemon. Nohup the daemon process so that it survives the death
  167.  * of its parent.
  168.  */
  169.  
  170. /*ARGSUSED*/
  171. static void ok_callback(dialog)
  172.     Widget            dialog;
  173. {
  174.     char            path[1024];
  175.     PID_T            pid;
  176.  
  177.     if (!find_file(path, DAEMON_FN, TRUE)) {
  178.         fprintf(stderr, "%s: WARNING: can't find %s\n",
  179.                             progname, DAEMON_FN);
  180.         create_nodaemon_popup();
  181.         return;
  182.     }
  183.     pid = fork();
  184.     if (pid > 0)
  185.         return;
  186.     if (pid == 0) {
  187.         fprintf(stderr, "%s: starting %s\n", progname, DAEMON_FN);
  188. #if defined(BSD) || defined(MIPS)
  189.         setpgrp(0, 0);
  190. #else
  191.         setsid();
  192. #endif
  193.         execl(path, DAEMON_FN, 0);
  194.     }
  195.     fprintf(stderr, "%s: WARNING: can't start %s: ", progname, DAEMON_FN);
  196.     perror("");
  197.     create_nodaemon_popup();
  198. }
  199.  
  200.  
  201. /*
  202.  * don't start the daemon, and set the flag that prevents the popup in the
  203.  * future.
  204.  */
  205.  
  206. /*ARGSUSED*/
  207. static void cancel_callback(dialog)
  208.     Widget            dialog;
  209. {
  210.     suppress_daemon_warning = True;
  211. }
  212.  
  213.  
  214. /*---------------------------------------------------------- multiple plan's */
  215. static char multiple_message[] = "\
  216. Warning:\n\
  217. Another %s program is running.\n\
  218. %s\n\
  219. Press Continue to start up anyway, or Kill to\n\
  220. attempt to kill the other program. Continuing\n\
  221. may cause loss of new appointment entries, and\n\
  222. command-line entry of appointments will not work.";
  223.  
  224. static void kill_callback();
  225.  
  226.  
  227. create_multiple_popup(nolock)
  228.     BOOL            nolock;        /* tried to kill competitor */
  229. {
  230.     char            msg[512];
  231.     Widget            dialog;
  232.     XmString        s1, s2, s3;
  233.     Arg            args[10];
  234.     int            n;
  235.  
  236.     sprintf(msg, multiple_message, progname,
  237.         nolock ? "The other program could not be killed.\n" : "");
  238.     s1 = XmStringCreateLtoR(msg, XmSTRING_DEFAULT_CHARSET);
  239.     s2 = XmStringCreateSimple("Kill");
  240.     s3 = XmStringCreateSimple("Continue");
  241.     n = 0;
  242.     XtSetArg(args[n], XmNmessageString,    s1);        n++;
  243.     XtSetArg(args[n], XmNokLabelString,    s2);        n++;
  244.     XtSetArg(args[n], XmNcancelLabelString,    s3);        n++;
  245.     dialog = XmCreateWarningDialog(mainwindow, "Warning", args, n);
  246.     XmStringFree(s1);
  247.     XmStringFree(s2);
  248.     XmStringFree(s3);
  249.     XtAddCallback(dialog, XmNokCallback, kill_callback, (XtPointer)NULL);
  250.     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
  251.     (void)XmInternAtom(display, "WM_DELETE_WINDOW", False);
  252.     XtManageChild(dialog);
  253. }
  254.  
  255.  
  256. /*
  257.  * The user pressed Kill. Try to acquire a lock.
  258.  */
  259.  
  260. /*ARGSUSED*/
  261. static void kill_callback(dialog)
  262.     Widget            dialog;
  263. {
  264.     if (!startup_lock(PLAN_PATH, TRUE))
  265.         create_multiple_popup(mainwindow);
  266. }
  267.